package com.banggood.upms.web;

import com.banggood.silk.web.api.exception.SkbException;
import com.banggood.upms.bean.exception.ExceptionEnum;
import com.banggood.upms.bean.po.UserAttr;
import com.banggood.upms.bean.vo.validation.AddGroup;
import com.banggood.upms.common.constant.UpmsConstant;
import com.banggood.upms.service.UserAttrService;
import com.banggood.upms.common.component.CommonComponent;
import io.swagger.annotations.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author XuChuangFeng
 */
@RequestMapping("/user-attr")
@RestController
@Api(value = "用户标签管理", description = "用户标签管理")
public class UserAttrController {

    protected final Log logger = LogFactory.getLog(getClass());
    @Autowired
    private UserAttrService userAttrService;
    @Autowired
    private CommonComponent commonComponent;


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "用户标签添加", notes = "返回成功或失败标志", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户主键", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "attrKey", value = "属性键", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "attrValue", value = "属性值", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 5000, message = "操作失败!")
    })
    public String addUserAttr(@Validated({AddGroup.class}) UserAttr po) throws Exception {
        po.setAttrId(commonComponent.generateId());
        int count = userAttrService.insert(po);
        if (count != 1) {
            throw new SkbException(ExceptionEnum.OPERATION_FAILED.getCode(), ExceptionEnum.OPERATION_FAILED.getMessage());
        }
        return UpmsConstant.SUCCESS;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation(value = "用户标签删除", notes = "返回成功或失败标志", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 5000, message = "操作失败!")
    })
    public String removeUserAttr(@RequestParam String id) throws Exception {
        int count = userAttrService.deleteByPrimaryKey(id);
        if (count != 1) {
            throw new SkbException(ExceptionEnum.OPERATION_FAILED.getCode(), ExceptionEnum.OPERATION_FAILED.getMessage());
        }
        return UpmsConstant.SUCCESS;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    @ApiOperation(value = "用户标签查看", notes = "根据userId返回用户所有的标签", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
    })
    public List<UserAttr> selectUserAttr(@RequestParam String id) {
        List<UserAttr> userAttrs = userAttrService.selectByUserPrimaryKey(id);
        return userAttrs;
    }
}
